package com.controller;


;
import com.controller.reqdto.DataQueryParam;
import com.controller.reqdto.GroupQueryParam;
import com.controller.reqdto.HeartReportParam;
import com.controller.respdto.DataQueryResp;
import com.controller.respdto.GroupList;
import com.controller.respdto.GroupListParam;
import com.controller.template.ResultHelper;

import com.controller.template.ResultInfo;
import com.repository.mapper.GroupMsgMapper;
import com.repository.mapper.PcGroupRelationshipMapper;
import com.repository.mapper.PcMessageMapper;

import com.service.GroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;
import java.util.Objects;


/**
 * @author ruiwen
 */
@RestController
@Api(tags = "数据查询模块")
@CrossOrigin
@RequestMapping("/data")
@Slf4j
public class DataController extends ResultHelper {

    @Resource
    GroupService groupService;

    @ApiOperation("运行数据获取（带分页）")
    @PostMapping("/runDataQuery")
    public ResultInfo runDataQuery(@RequestBody DataQueryParam dataQueryParam) {

        DataQueryResp dataQueryResp = new DataQueryResp();
        GroupQueryParam queryParam = new GroupQueryParam();
        queryParam.setStartTime(dataQueryParam.getStartTime());
        queryParam.setEndTime(dataQueryParam.getEndTime());
        GroupList groupList = groupService.queryGroup(queryParam);
        if (Objects.isNull(groupList) || CollectionUtils.isEmpty(groupList.getGroupLists())) {
            return successResultInfo(DataQueryResp.builder()
                    .groupList(null)
                    .runNum(0)
                    .failProbability(BigDecimal.ZERO)
                    .successProbability(BigDecimal.ZERO)
                    .personNum(0)
                    .build());
        }

        dataQueryResp.setRunNum(groupList.getGroupLists().size());

        Integer personNum = 0;
        List<GroupListParam> groupLists = groupList.getGroupLists();
        for (GroupListParam list : groupLists) {
            if (!CollectionUtils.isEmpty(list.getPcMessages())) {
                personNum += list.getPcMessages().size();
            }
        }
        dataQueryResp.setPersonNum(personNum);
        // 计算成功率 失败率
        Integer successNum = 0;
        Integer failNum = 0;
        for (GroupListParam groupListParam : groupLists) {
            if (Objects.nonNull(groupListParam.getGroupMsg().getContentProgress()) && groupListParam.getGroupMsg().getContentProgress().compareTo(BigDecimal.valueOf(100.00)) == 0) {
                successNum++;
            } else {
                failNum++;
            }
        }
        dataQueryResp.setSuccessProbability(BigDecimal.valueOf(successNum).divide(BigDecimal.valueOf(groupLists.size()), 2, RoundingMode.HALF_UP));
        dataQueryResp.setFailProbability(BigDecimal.valueOf(failNum).divide(BigDecimal.valueOf(groupLists.size()), 2, RoundingMode.HALF_UP));
        queryParam.setLimit(dataQueryParam.getLimit());
        queryParam.setPage(dataQueryParam.getPage());
        groupList = groupService.queryGroup(queryParam);
        dataQueryResp.setGroupList(groupList);
        return successResultInfo(dataQueryResp);
    }

}
